package com.alpaak.common.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JedisUtils {

	private static String redisIp = "127.0.0.1";

	private static Integer port = 6379;

	public static void set(String key ,String value) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			jedis.set(key,value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//jedisPool.returnResource(jedis);
			jedis.close(); // 关闭
		}
	}
	
	public static String get(String key) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		String value = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			value = jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jedis.close(); // 关闭
		}
		return value;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> Object get(String key, Class<T> class1) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		T t = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			
			String value = jedis.get(key);
			t = (T) SerializeUtil.unserialize(value.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jedis.close(); // 关闭
		}
		return t;
	}
	
	public static Long delete(String key) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		Long del = 0L;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			del = jedis.del(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jedis.close(); // 关闭
		}
		return del;
	}
	
	public static void set(String key ,Object obj) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			
			jedis.set(key,SerializeUtil.serialize(obj).toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//jedisPool.returnResource(jedis);
			jedis.close(); // 关闭
		}
	}
	
	public static void set(String key ,Object obj ,Integer time) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			jedis.setex(key, time*3600 , SerializeUtil.serialize(obj).toString());
			jedis.set(key,SerializeUtil.serialize(obj).toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//jedisPool.returnResource(jedis);
			jedis.close(); // 关闭
		}
	}
	
	public static void hmset(String hash ,String key ,String value) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			
			Map<String,String> map = new HashMap<String,String>();
			map.put(key, value);
			jedis.hmset(hash,map);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			//jedisPool.returnResource(jedis);
			jedis.close(); // 关闭
		}
	}
	
	public static List<String> hmget(String hash ,String key) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		List<String> values = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			
			values = jedis.hmget(hash,key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jedis.close(); // 关闭
		}
		return values;
	}
	
	public static Map<String, String> hgetAll(String hash) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		Map<String, String> values = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			
			values = jedis.hgetAll(hash);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jedis.close(); // 关闭
		}
		return values;
	}
	
	public static Long lpush(String key,String value) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		Long values = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			
			values = jedis.lpush(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jedis.close(); // 关闭
		}
		return values;
	}
	
	public static Long rpush(String key,String value) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		Long values = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			
			values = jedis.rpush(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jedis.close(); // 关闭
		}
		return values;
	}
	
	public static String lpop(String key) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		String values = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			
			values = jedis.lpop(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jedis.close(); // 关闭
		}
		return values;
	}
	
	public static String rpop(String key) {
		JedisPool jedisPool = null;
		Jedis jedis = null;
		String values = null;
		try {
			JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
			jedisPoolConfig.setMaxTotal(200);// 设置最大连接数
			jedisPool = new JedisPool(jedisPoolConfig, redisIp, port);
			jedis = jedisPool.getResource();
			
			values = jedis.rpop(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jedis.close(); // 关闭
		}
		return values;
	}
	
	
	
	

}
